unit TemeljnicaRDM;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
  DBClient, StdVcl, FMTBcd, Provider, alProvider, DB, DBXCommon,
  SqlExpr, Variants, DokumentStatus, ActiveX, StrUtils,
  PoslovanjeSrv_TLB,
  BaseRDM, ObjektRDM, DokumentRDM;

type
  ETransferError = class(EPoslovanjeError);

  TrdmTemeljnica = class(TrdmDokument, IrdmTemeljnica)
    dspPostavka: TBaseDataSetProvider;
    dsPostavka: TSQLDataSet;
    procedure BeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet;
      DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
      var Applied: Boolean);
  private
    procedure Prenos(const Session: ISession; SelectionType: Integer;
      StevilkeDokumentov: OleVariant; SelectionSQL: string; ProcName: string; Storno: Boolean);
  protected
    class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;
    function GetSelectCommandText(DataSet: TSQLDataSet): string; override;
    function GetTableName: string; override;
    function Get_RazredID(const ProviderName: WideString): Integer; safecall;
    function Get_Saldo(ID: Integer): Double; safecall;
    procedure PrenosGlavnaKnjiga(const Session: ISession; SelectionType: Integer;
      StevilkeDokumentov: OleVariant); safecall;
    procedure PrenosKnjigaIzdanihRacunov(const Session: ISession;
      SelectionType: Integer; StevilkeDokumentov: OleVariant); safecall;
    procedure PrenosKnjigaPrejetihRacunov(const Session: ISession;
      SelectionType: Integer; StevilkeDokumentov: OleVariant); safecall;
    procedure RefreshRecord(const Session: ISession;
      const ProviderName: WideString; ID: Integer); safecall;
    procedure Renumber(const Session: ISession; const FieldName,
      SortFieldName: WideString; StartValue, EndValue: Integer); safecall;
    procedure StornoPrenosGlavnaKnjiga(const Session: ISession; SelectionType: Integer;
      StevilkeDokumentov: OleVariant); safecall;
    procedure StornoPrenosKnjigaIzdanihRacunov(const Session: ISession;
      SelectionType: Integer; StevilkeDokumentov: OleVariant); safecall;
    procedure StornoPrenosKnjigaPrejetihRacunov(const Session: ISession;
      SelectionType: Integer; StevilkeDokumentov: OleVariant); safecall;
    procedure SetDefaultExpression(DataSet: TDataSet); override;
    procedure Set_ReadOnly(Value: WordBool); safecall;
    procedure Set_SQLConnection(Value: Integer); safecall;
  public
    { Public declarations }
  end;

var
  TemeljnicaFactory: TComponentFactory;

resourcestring
  TransferError = 'Prenos temeljnice %d ni uspel.' + #13#10 + '%s';

implementation

{$R *.DFM}

procedure TrdmTemeljnica.BeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet;
  UpdateKind: TUpdateKind; var Applied: Boolean);
var
  ID: TField;
  PostavkaID: TField;
begin
  inherited;
  if (Sender = dspPostavka) then
  begin
    ID := DeltaDS.FieldByName(KeyFieldName);
    PostavkaID := DeltaDS.FieldByName('POSTAVKA_ID');
    if (UpdateKind = ukInsert) then
    begin
      if VarIsEmpty(PostavkaID.NewValue) or VarIsNull(PostavkaID.NewValue) or (PostavkaID.NewValue < 0) then
        PostavkaID.NewValue := ID.NewValue;
    end
    else if (UpdateKind = ukModify) then
    begin
      if VarIsNull(PostavkaID.NewValue) then
        PostavkaID.NewValue := ID.OldValue;
    end;
  end;
end;

function TrdmTemeljnica.GetSelectCommandText(DataSet: TSQLDataSet): string;
var
  SQL: TStringList;
begin
  SQL := TStringList.Create;
  try
    if DataSet = dsMain then
    begin
      SQL.Add('select');
        SQL.Add('TEM.*,');
        SQL.Add('STE.NAZIV as SKUPINA_NAZIV,');
        SQL.Add('STE.OZNAKA as SKUPINA_OZNAKA,');
        SQL.Add('TSA.SALDO as SALDO');
      SQL.Add('from TEMELJNICA TEM');
      SQL.Add('inner join SKUPINA_TEMELJNICA STE on (TEM.SKUPINA_ID = STE.ID)');
      SQL.Add('left outer join TEM_GET_SALDO(TEM.ID) TSA on (TEM.ID = TSA.ID)');
      SQL.Add('where TEM.REF_ID = :POSLOVNO_LETO_ID');
    end
    else if DataSet = dsPostavka then
    begin
      SQL.Add('select');
        SQL.Add('TPO.*,');
        SQL.Add('KON.SIFRA as KONTO_SIFRA,');
        SQL.Add('KON.NAZIV as KONTO_NAZIV,');
        SQL.Add('KON.RAZRED_ID as KONTO_RAZRED_ID,');
        SQL.Add('SIF.NAZIV as SIFRANT_NAZIV,');
        SQL.Add('TST.STEVILKA as POSTAVKA_STEVILKA');
      SQL.Add('from TEMELJNICA_POSTAVKA TPO');
      SQL.Add('inner join KONTO KON on (TPO.KONTO_ID = KON.ID)');
      SQL.Add('left outer join SIFRANT SIF on (TPO.SIFRANT_ID = SIF.ID)');
      SQL.Add('left outer join TPO_GET_STEVILKA(TPO.POSTAVKA_ID) TST on (TPO.POSTAVKA_ID = TST.POSTAVKA_ID)');
      SQL.Add('where TPO.REF_ID = :REF_ID');
    end;
  finally
    Result := Trim(SQL.Text);
    SQL.Free;
  end;
end;

function TrdmTemeljnica.GetTableName: string;
begin
  Result := 'TEM';
end;

function TrdmTemeljnica.Get_RazredID(
  const ProviderName: WideString): Integer;
begin
  Result := inherited Get_RazredID(ProviderName);
end;

function TrdmTemeljnica.Get_Saldo(ID: Integer): Double;
var
  DBXTrans : TDBXTransaction;
  DataSet: TSQLDataSet;
begin
  DBXTrans := SQLConnection.BeginTransaction(TDBXIsolations.RepeatableRead);
  try
    DataSet := TSQLDataSet.Create(nil);
    try
      DataSet.SQLConnection := SQLConnection;
      DataSet.CommandText := 'SELECT * FROM TEM_GET_SALDO(:PAR_ID)';
      DataSet.ParamByName('PAR_ID').Value := ID;
      DataSet.Open;
      while not DataSet.Eof do
      begin
        Result := DataSet.FieldByName('SALDO').AsFloat;
        DataSet.Next;
      end;
      DataSet.Close;
    finally
      DataSet.Free;
    end;
    SQLConnection.CommitFreeAndNil(DBXTrans);
  except
    SQLConnection.RollBackFreeAndNil(DBXTrans);
    raise;
  end;
end;

procedure TrdmTemeljnica.Prenos(const Session: ISession; SelectionType: Integer;
  StevilkeDokumentov: OleVariant; SelectionSQL: string; ProcName: string; Storno: Boolean);
var
  DBXTrans : TDBXTransaction;
  Proc: TSQLStoredProc;
  //Proc: TSQLDataSet;
  DataSet: TSQLDataSet;
  PoslovnoLetoID: Variant;
  ID: Integer;
  StevilkaDokumenta: Integer;
begin
  StevilkaDokumenta := -1;
  DBXTrans := SQLConnection.BeginTransaction(TDBXIsolations.RepeatableRead);
  try
    DataSet := TSQLDataSet.Create(nil);
    //Proc := TSQLDataSet.Create(nil);
    Proc := TSQLStoredProc.Create(nil);
    PoslovnoLetoID := Session.Params['POSLOVNO_LETO_ID'];
    try
      DataSet.SQLConnection := SQLConnection;
      DataSet.CommandType := ctQuery;
      case SelectionType of
        1: DataSet.CommandText :=
             Format(SelectionSQL + ' and (TEM.REF_ID = %s) and (TEM.STEVILKA = %s)', [PoslovnoLetoID, VarArrayGet(StevilkeDokumentov, [0])]);
        2: DataSet.CommandText :=
             Format(SelectionSQL + ' and (TEM.REF_ID = %s) and (TEM.STEVILKA between %s and %s)', [PoslovnoLetoID, VarArrayGet(StevilkeDokumentov, [0]), VarArrayGet(StevilkeDokumentov, [1])]);
      end;
      DataSet.Open;
      Proc.SQLConnection := SQLConnection;
      //Proc.CommandType := ctStoredProc;
      Proc.CommandText := ProcName;
      while not DataSet.Eof do
      begin
        ID := DataSet.FieldByName('ID').AsInteger;
        StevilkaDokumenta := DataSet.FieldByName('Stevilka').AsInteger;
        Proc.ParamByName('PAR_ID').Value := ID;
        if Storno then
          Proc.ParamByName('PAR_STATUS_BIT_VALUE').Value := 0
        else
          Proc.ParamByName('PAR_STATUS_BIT_VALUE').Value := 1;
        DataSet.Next;
        //Proc.ExecSQL();
        Proc.ExecProc;
      end;
      DataSet.Close;
    finally
      Proc.Free;
      DataSet.Free;
    end;
    SQLConnection.CommitFreeAndNil(DBXTrans);
  except
    on E: Exception do
    begin
      SQLConnection.RollBackFreeAndNil(DBXTrans);
      E.Message := AnsiReplaceStr(E.Message, 'exception 4', '');
      raise ETransferError.CreateFmt(TransferError, [StevilkaDokumenta, E.Message]);
    end;
  end;
end;

procedure TrdmTemeljnica.PrenosGlavnaKnjiga(const Session: ISession;
  SelectionType: Integer; StevilkeDokumentov: OleVariant);
var
  SelectionSQL: TStringList;
begin
  SelectionSQL := TStringList.Create;
  try
    SelectionSQL.Add('select');
      SelectionSQL.Add('TEM.ID, TEM.STEVILKA');
    SelectionSQL.Add('from TEMELJNICA TEM');
    SelectionSQL.Add('where');
      SelectionSQL.Add('(BIN_AND(TEM.STATUS, 1) = 0)');
    Prenos(Session, SelectionType, StevilkeDokumentov, SelectionSQL.Text, 'TEM_TRN_GKN', False);
  finally
    SelectionSQL.Free;
  end;
end;

procedure TrdmTemeljnica.PrenosKnjigaIzdanihRacunov(const Session: ISession;
  SelectionType: Integer; StevilkeDokumentov: OleVariant);
var
  SelectionSQL: TStringList;
begin
  SelectionSQL := TStringList.Create;
  try
    SelectionSQL.Add('select');
      SelectionSQL.Add('TEM.ID, TEM.STEVILKA');
    SelectionSQL.Add('from TEMELJNICA TEM');
    SelectionSQL.Add('where');
      SelectionSQL.Add('(TEM.SKUPINA_ID = 85) and');
      SelectionSQL.Add('(BIN_AND(TEM.STATUS, 2) = 0)');
    Prenos(Session, SelectionType, StevilkeDokumentov, SelectionSQL.Text, 'TEM_TRN_KIR', False);
  finally
    SelectionSQL.Free;
  end;
end;

procedure TrdmTemeljnica.PrenosKnjigaPrejetihRacunov(const Session: ISession;
  SelectionType: Integer; StevilkeDokumentov: OleVariant);
var
  SelectionSQL: TStringList;
begin
  SelectionSQL := TStringList.Create;
  try
    SelectionSQL.Add('select');
      SelectionSQL.Add('TEM.ID, TEM.STEVILKA');
    SelectionSQL.Add('from TEMELJNICA TEM');
    SelectionSQL.Add('where');
      SelectionSQL.Add('(TEM.SKUPINA_ID = 79) and');
      SelectionSQL.Add('(BIN_AND(TEM.STATUS, 4) = 0)');
    Prenos(Session, SelectionType, StevilkeDokumentov, SelectionSQL.Text, 'TEM_TRN_KPR', False);
  finally
    SelectionSQL.Free;
  end;
end;

procedure TrdmTemeljnica.RefreshRecord(const Session: ISession;
  const ProviderName: WideString; ID: Integer);
begin
  inherited;
end;

procedure TrdmTemeljnica.Renumber(const Session: ISession; const FieldName,
  SortFieldName: WideString; StartValue, EndValue: Integer);
begin
  inherited;
end;

procedure TrdmTemeljnica.StornoPrenosGlavnaKnjiga(const Session: ISession;
  SelectionType: Integer; StevilkeDokumentov: OleVariant);
var
  SelectionSQL: TStringList;
begin
  SelectionSQL := TStringList.Create;
  try
    SelectionSQL.Add('select');
      SelectionSQL.Add('TEM.ID, TEM.STEVILKA');
    SelectionSQL.Add('from TEMELJNICA TEM');
    SelectionSQL.Add('where');
      SelectionSQL.Add('(BIN_AND(TEM.STATUS, 1) = 1)');
    Prenos(Session, SelectionType, StevilkeDokumentov, SelectionSQL.Text, 'TEM_TRN_GKN', True);
  finally
    SelectionSQL.Free;
  end;
end;

procedure TrdmTemeljnica.StornoPrenosKnjigaIzdanihRacunov(
  const Session: ISession; SelectionType: Integer;
  StevilkeDokumentov: OleVariant);
var
  SelectionSQL: TStringList;
begin
  SelectionSQL := TStringList.Create;
  try
    SelectionSQL.Add('select');
      SelectionSQL.Add('TEM.ID, TEM.STEVILKA');
    SelectionSQL.Add('from TEMELJNICA TEM');
    SelectionSQL.Add('where');
      SelectionSQL.Add('(TEM.SKUPINA_ID = 85) and');
      SelectionSQL.Add('(BIN_AND(TEM.STATUS, 2) = 2)');
    Prenos(Session, SelectionType, StevilkeDokumentov, SelectionSQL.Text, 'TEM_TRN_KIR', True);
  finally
    SelectionSQL.Free;
  end;
end;

procedure TrdmTemeljnica.StornoPrenosKnjigaPrejetihRacunov(
  const Session: ISession; SelectionType: Integer;
  StevilkeDokumentov: OleVariant);
var
  SelectionSQL: TStringList;
begin
  SelectionSQL := TStringList.Create;
  try
    SelectionSQL.Add('select');
      SelectionSQL.Add('TEM.ID, TEM.STEVILKA');
    SelectionSQL.Add('from TEMELJNICA TEM');
    SelectionSQL.Add('where');
      SelectionSQL.Add('(TEM.SKUPINA_ID = 79) and');
      SelectionSQL.Add('(BIN_AND(TEM.STATUS, 4) = 4)');
    Prenos(Session, SelectionType, StevilkeDokumentov, SelectionSQL.Text, 'TEM_TRN_KPR', True);
  finally
    SelectionSQL.Free;
  end;
end;

procedure TrdmTemeljnica.SetDefaultExpression(DataSet: TDataSet);
begin
  inherited;
  if DataSet = dsPostavka then
  begin
    with DataSet do
    begin
      FieldByName('STEVILKA_SKUPINE').DefaultExpression := '1';
      FieldByName('DEBET').DefaultExpression := '0';
      FieldByName('KREDIT').DefaultExpression := '0';
    end
  end;
end;

procedure TrdmTemeljnica.Set_ReadOnly(Value: WordBool);
begin
  inherited;
end;

procedure TrdmTemeljnica.Set_SQLConnection(Value: Integer);
begin
  inherited;
end;

class procedure TrdmTemeljnica.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
begin
  if Register then
  begin
    inherited UpdateRegistry(Register, ClassID, ProgID);
    EnableSocketTransport(ClassID);
    EnableWebTransport(ClassID);
  end else
  begin
    DisableSocketTransport(ClassID);
    DisableWebTransport(ClassID);
    inherited UpdateRegistry(Register, ClassID, ProgID);
  end;
end;

initialization
  TemeljnicaFactory := TComponentFactory.Create(ComServer, TrdmTemeljnica,
    Class_rdmTemeljnica, ciInternal, tmApartment);
end.
